package com.soft.servlet;

import com.soft.entity.Order;
import com.soft.service.Domain;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName GuanBiServlet
 * @Description 订单关闭servlet
 * @Date 2021/5/31 20:47
 * @Version 1.0
 */
@WebServlet(name = "GuanBiServlet", urlPatterns = "/GuanBi")
public class GuanBiServlet extends HttpServlet {

    private Domain domain;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        domain = new Domain();

        int orderId = Integer.parseInt(req.getParameter("orderId"));
        Order order = domain.getOrderById(orderId);

        if (order.getStatus() == Order.STATUS.STATUS_CLOSED) {
            req.getSession().setAttribute("flag", 23);
            req.getRequestDispatcher("/OrderManageList").forward(req, resp);
        } else {
            req.setAttribute("Order", order);
            req.getRequestDispatcher("guanbi.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        domain = new Domain();

        int orderId = Integer.parseInt(req.getParameter("member.id"));
        String detail = req.getParameter("member.closeDetail");

        Order order = domain.getOrderById(orderId);
        order.setCloseDetail(detail);
        order.setStatus(String.valueOf(Order.STATUS.STATUS_CLOSED));

        if (domain.updateOrder(order))
            req.getSession().setAttribute("flag", 21);
        else
            req.getSession().setAttribute("flag", 22);
        req.setAttribute("orderId", orderId);
        req.setAttribute("Order", order);
        req.getRequestDispatcher("guanbi.jsp").forward(req, resp);
    }
}
